from django.shortcuts import render, redirect
from django.http import HttpResponse
from functools import wraps

import DLT.getweb, SSQ.getweb
import lottery_V2.export_and_import_excel as excelei

# 定时启动程序
# 定时更新系统数据，并检测当前数据库中未核实投注数据是否中奖，如中奖，调用短信通知平台进行通知

from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_job  # register_events

# 定时调度发送中奖通知短信，仅10条免费，测试用，可删除

try:
    hour_t = '06'
    minute_t = '00'
    second_t = '00'
    # 实例化调度器
    scheduler = BackgroundScheduler()  # 在后台运行调度，不影响当前的系统计算运行
    # 调度器使用DjangoJobStore()
    scheduler.add_jobstore(DjangoJobStore(), "default")

    # 'cron'方式循环，周一到周五day_of_week='mon-fri'，每天22:30:00执行,id为工作ID作为标记
    # @register_job(scheduler, 'cron', day_of_week='mon-sun', hour=hour_t, minute=minute_t, id='pre_analysis')  # 每天自动更新

    '''
        year (int|str) – 4-digit year
        month (int|str) – month (1-12)
        day (int|str) – day of the (1-31)
        week (int|str) – ISO week (1-53)
        day_of_week (int|str) – number or name of weekday (0-6 or mon,tue,wed,thu,fri,sat,sun)
        hour (int|str) – hour (0-23)
        minute (int|str) – minute (0-59)
        econd (int|str) – second (0-59)

        start_date (datetime|str) – earliest possible date/time to trigger on (inclusive)
        end_date (datetime|str) – latest possible date/time to trigger on (inclusive)
        timezone (datetime.tzinfo|str) – time zone to use for the date/time calculations (defaults to scheduler timezone)

        *    any    Fire on every value
        */a    any    Fire every a values, starting from the minimum
        a-b    any    Fire on any value within the a-b range (a must be smaller than b)
        a-b/c    any    Fire every c values within the a-b range
        xth y    day    Fire on the x -th occurrence of weekday y within the month
        last x    day    Fire on the last occurrence of weekday x within the month
        last    day    Fire on the last day within the month
        x,y,z    any    Fire on any matching expression; can combine any number of any of the above expressions
    '''


    def test_job():
        DLT.getweb.get_history_phasedata_from_360cp()  # 每天六点更新
        SSQ.getweb.get_history_phasedata_from_360cp()  # 每天六点更新


    scheduler.add_job(test_job, 'cron',  hour=hour_t, minute=minute_t, id='refresh_data')
    # def test_job():
    #     print('执行任务')

    # scheduler.add_job(test_job, 'cron', day_of_week='mon-sun', hour=hour_t, minute=minute_t, id='lottry_auto_check')

    # 监控任务
    # register_events(scheduler)
    # 调度器开始
    scheduler.start()
except Exception as e:
    print(e)
    # scheduler.shutdown()


def index(request):
    if request.method == "GET":
        if request.is_ajax():
            if request.GET.get("cmd_class") == "query":
                command = request.GET.get("command")
                if command == 'refresh_all_history_data':  # 获取历史开奖数据，默认返回近100期
                    DLT.getweb.get_history_phasedata_from_360cp()  # 不需要返回
                    SSQ.getweb.get_history_phasedata_from_360cp()  # 不需要返回
                    return HttpResponse('yes')
                if command == 'download_excel':
                    result = excelei.export_excel('backup')#直接指定为backup防止服务器磁盘被占用太多，安全角度
                    return HttpResponse(result)
                if command == 'upload_excel':#上传数据
                    return HttpResponse('yes')

    http = render(request, 'index.html')
    return HttpResponse(http)
